﻿using PBO.Data;
using PBO.Game.BattleClass;
using PBO.Network;
using PBO.Network.Enumerations;
using System;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;

namespace PBO.Game.Img
{
    public partial class SelectTargetForm : Form
    {
        public SelectTargetForm()
        {
            InitializeComponent();
        }

        private BattleGroup myTeam;
        private BattleGroup opponentTeam;

        private TargetIndex mTarget;
        private Rectangle opponent1 = new Rectangle(4, 12, 113, 65);
        private Rectangle opponent2 = new Rectangle(140, 12, 113, 65);
        private Rectangle teamFriend;

        private Rectangle self;
        public TargetIndex Target
        {
            get { return mTarget; }
        }

        public SelectTargetForm(BattleGroup myTeamValue, Pokemon user, bool selfEff)
        {
            InitializeComponent();
            myTeam = myTeamValue;
            opponentTeam = myTeamValue.Opponent;
            if (object.ReferenceEquals(user, myTeamValue.SelectedPokemon.ElementAt(0)))
            {
                if (selfEff)
                    self = new Rectangle(4, 93, 112, 47);
                if (myTeamValue.SelectedPokemon.ElementAt(1).HealthPower != 0)
                    teamFriend = new Rectangle(140, 93, 112, 47);
            }
            else
            {
                if (selfEff)
                    self = new Rectangle(140, 93, 112, 47);
                if (myTeamValue.SelectedPokemon.ElementAt(0).HealthPower != 0)
                    teamFriend = new Rectangle(4, 93, 112, 47);
            }
        }

        #region event

        private void SelectTargetForm_Load(object sender, EventArgs e)
        {
            Bitmap img = new Bitmap(Properties.Resources.SelectTarget);
            using (Graphics graph = Graphics.FromImage(img))
            {
                for (int i = 0; i <= 1; i++)
                {
                    var p1 = myTeam.SelectedPokemon.ElementAt(i);
                    var p2 = opponentTeam.SelectedPokemon.ElementAt(i);


                    if (p1.HealthPower != 0)
                    {
                        graph.DrawString(p1.NickName, NameFont, Brushes.Black, MyPoint[i, 0]);
                        if (p1.Gender == PokemonGender.Male)
                        {
                            graph.DrawImageUnscaled(Properties.Resources.male, MyPoint[i, 1]);
                        }
                        else if (p1.Gender == PokemonGender.Female)
                        {
                            graph.DrawImageUnscaled(Properties.Resources.female, MyPoint[i, 1]);
                        }
                    }
                    if (p2.HealthPower != 0)
                    {
                        graph.DrawImageUnscaled(p2.Icon, OpponentPoint[i, 1]);
                        graph.DrawString(p2.NickName, NameFont, Brushes.Black, OpponentPoint[i, 0]);
                        if (p2.Gender == PokemonGender.Male)
                        {
                            graph.DrawImageUnscaled(Properties.Resources.male, OpponentPoint[i, 2]);
                        }
                        else if (p2.Gender == PokemonGender.Female)
                        {
                            graph.DrawImageUnscaled(Properties.Resources.female, OpponentPoint[i, 2]);
                        }
                    }
                }
            }
            BackgroundImage = img;
        }

        private void SelectTargetForm_MouseClick(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                Rectangle cancel = new Rectangle(10, 157, 236, 35);
                if (cancel.Contains(e.Location))
                {
                    this.DialogResult = DialogResult.Cancel;
                    Close();
                }
                else if (self.Contains(e.Location))
                {
                    this.DialogResult = DialogResult.OK;
                    mTarget = TargetIndex.Self;
                }
                else if (opponent1.Contains(e.Location))
                {
                    if (opponentTeam.SelectedPokemon.ElementAt(0).HealthPower == 0)
                        return;
                    this.DialogResult = DialogResult.OK;
                    mTarget = TargetIndex.Opponent1;
                }
                else if (opponent2.Contains(e.Location))
                {
                    if (opponentTeam.SelectedPokemon.ElementAt(1).HealthPower == 0)
                        return;
                    this.DialogResult = DialogResult.OK;
                    mTarget = TargetIndex.Opponent2;
                }
                else if (teamFriend.Contains(e.Location))
                {
                    this.DialogResult = DialogResult.OK;
                    mTarget = TargetIndex.TeamFriend;
                }
            }
        }

        #endregion

        #region static

        private static readonly Point[,] OpponentPoint = new Point[,] 
        {
        { 
            new Point(13, 56), 
            new Point(43, 21),  
            new Point(98, 61) 
        },
        { 
            new Point(150, 56),
            new Point(180, 21), 
            new Point(235, 61)
        }
        };

        private static readonly Point[,] MyPoint = new Point[,] {
		{
			new Point(13, 113),
			new Point(98, 118)
		},
		{
			new Point(149, 113),
			new Point(234, 118)
		}
        };

        private static Font NameFont = new Font(ImageHelper.FONT_NAME, 16, FontStyle.Regular, GraphicsUnit.Pixel);

        #endregion

    }
}
